package com.small.commom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.widget.Toast;

import com.small.db.DbHelper;
import com.small.db.DbTool;
import com.small.model.DiscountDetailModel;
import com.small.model.DiscountListModel;
import com.small.model.DiscountModel;
import com.small.model.FavDiscountModel;
import com.small.model.FavShopModel;
import com.small.model.SQChildModel;
import com.small.model.SQParentModel;
import com.small.model.ShopBaseInfoModel;
import com.small.model.ShopChildModel;
import com.small.model.ShopNewsModel;
import com.small.model.ShopParentModel;
import com.small.model.userModel;
import com.small.view.LoadingDailog;
import com.small.view.custview.dialog.SingleChoicePopWindow;

public class Action {

	private Context ctx;
	LoadingDailog dialog;
	protected SingleChoicePopWindow singleChoicePopWindow;

	public Action(Context ctxs) {
		this.ctx = ctxs; 
	}
	
	    static double DEF_PI = 3.14159265359; // PI
	 	static double DEF_2PI= 6.28318530712; // 2*PI
	 	static double DEF_PI180= 0.01745329252; // PI/180.0
	 	static double DEF_R =6370693.5; // radius of earth
	 	public static int GetShortDistance(double lon1, double lat1, double lon2, double lat2)
	 	{
	 		double ew1, ns1, ew2, ns2;
	 		double dx, dy, dew;
	 		double distance;
	 		// 角度转换为弧度
	 	    ew1 = lon1 * DEF_PI180;
	 		ns1 = lat1 * DEF_PI180;
	 	    ew2 = lon2 * DEF_PI180;
	 	    ns2 = lat2 * DEF_PI180;
	 		// 经度差
	 		dew = ew1 - ew2;
	 		// 若跨东经和西经180 度，进行调整
	 		if (dew > DEF_PI)
	 		dew = DEF_2PI - dew;
	     	else if (dew < -DEF_PI)
	 		dew = DEF_2PI + dew;
	 		dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
	 	    dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
	 		// 勾股定理求斜边长
	 		distance = Math.sqrt(dx * dx + dy * dy);
	 		return (int) distance;
	 	}

	// 注销
	public void logOut() {
		SharedPreferences sp = ctx
				.getSharedPreferences(Common.Userbase_Info, 0);
		Editor spEd = sp.edit();
		spEd.putBoolean("isLogin", false);
		spEd.putString("UserName", "");
		spEd.putString("Pass", "");
		spEd.commit();
		Common.userid = "";
		Common.userName = "";
		Common.model = null;
	}

	// 保存用户信息
	public Thinksns getInstance() {
		return Thinksns.getInstance(ctx);
	}

	// 保存用户信息
	public void saveUserInfo(String strName, String strPass, boolean isAutoLogin) {
		Thinksns.getInstance(ctx).saveUserInfo(strName, strPass, isAutoLogin);
	}

	public void showProgressDialog(String dialogMsg) {
		if (dialog != null && dialog.isShowing()) {
			dialog.UpMsg(dialogMsg);
		} else {
			dialog = new LoadingDailog(ctx, dialogMsg);
			dialog.show();
		}
	}

	public void stopProgressDialog() {
		if (ctx != null) {
			if (dialog != null) {
				if (dialog.isShowing()) {
					dialog.cancel();
				}
				dialog = null;
			}
		}
	}

	public void showChoicePopWindow(String[] itmes, String title) {
		this.singleChoicePopWindow = new SingleChoicePopWindow(ctx);
		if (!singleChoicePopWindow.isShowing()) {
			singleChoicePopWindow.initChoicePopWindow(itmes, title);
			singleChoicePopWindow.show();
		}
	}

	public void dismissChoicePopWindow() {
		if (singleChoicePopWindow.isShowing()) {
			singleChoicePopWindow.dismiss();
		}
	}

	public void showLongToast(String toastMsg) {
		Toast.makeText(ctx, toastMsg, Toast.LENGTH_SHORT).show();
	}

	public void showShortToast(String toastMsg) {
		Toast.makeText(ctx, toastMsg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 登录
	 * 
	 * @param strName
	 *            :用户名
	 * @param strPass
	 *            ： 密码
	 * @return 1：登录成功，0：用户名或密码错误 -1：登录失败 -2:网络异常、登录失败
	 */
	public int doLogin(String strName, String strPass) {
		int result = -1;
		try {

			StringBuilder sb = new StringBuilder().append(Common.LoginUrl);
			sb.append("&userName=" + TextUtil.toWeb(strName));
			sb.append("&pass=" + strPass);
			String resultStr = HttpUtil.CallMethod(sb.toString());
			if (!resultStr.equals("") && resultStr != null) {
				JSONObject json = HttpUtil.toJsonObject(resultStr);
				if (json != null) {
					if (json.getString("resCode").equals("1")) {
						json = HttpUtil
								.toJsonObject(json.getString("userInfo"));
						userModel model = new userModel();
						model.setAddress(json.getString("Address"));
						model.setBirthday(json.getString("Birthday"));
						model.setCity(json.getString("City"));
						model.setEmail(json.getString("Email"));
						model.setFreezeFlag(json.getString("FreezeFlag"));
						model.setJiFen(json.getString("JiFen"));
						model.setMemberID(json.getString("MemberID"));
						Common.userid = json.getString("MemberID");
						model.setMemberName(json.getString("MemberName"));
						model.setNickName(json.getString("NickName"));
						model.setPassword(json.getString("Password"));
						model.setRegTime(json.getString("RegTime"));
						model.setSex(TextUtil.onWeb(json.getString("Sex")));
						model.setTel(json.getString("Tel"));
						model.setUserImageBig(json.getString("UserImageBig"));
						model.setUserImageSmall(json
								.getString("UserImageSmall"));
						model.setUserName(TextUtil.onWeb(json
								.getString("UserName")));
						model.setUserType(json.getString("UserType"));
						Common.model = model;
						return result = 1;
					} else {
						return result = 0;
					}
				}
			}
		} catch (Exception e) {
			return result = -2;
		}
		return result;
	}

	/**
	 * 注册
	 * 
	 * @param strName
	 *            :用户名
	 * @param strPass
	 *            ： 密码
	 * @param strPhone
	 *            :手机号
	 * @param strNick
	 *            ： 昵称
	 * @return -1:注册成功，但是登录失败。 0 :注册失败，1: 注册成功，2: 用户已存在 -2:网络异常、登录失败
	 */
	public int doRegister(String strName, String strPass, String strPhone,
			String strNick) {
		int result = 0;
		try {
			StringBuilder sb = new StringBuilder().append(Common.RegisterUrl);
			sb.append("&userName=" + TextUtil.toWeb(strName));
			sb.append("&tel=" + strPhone);
			sb.append("&nickname=" + TextUtil.toWeb(strNick));
			sb.append("&pass=" + strPass);
			String resultStr = HttpUtil.CallMethod(sb.toString());

			if (!resultStr.equals("") && resultStr != null) {
				JSONObject json = HttpUtil.toJsonObject(resultStr);

				if (json != null) {

					if (json.getString("recCode").equals("1")) {
						Common.userid = json.getString("userID");
						return result = 1;
					} else if (json.getString("recCode").equals("2")) {
						return result = 2;
					}
				}
			}
		} catch (Exception e) {
			return result = -2;
		}
		return result;
	}

	/**
	 * 个人信息修改
	 * 
	 * @param userID
	 *            :用户ID
	 * @param nickname
	 *            ：昵称
	 * @param tel
	 *            :手机号
	 * @param sex
	 *            ： 性别
	 * @param birthday
	 *            ： 性别
	 * @param email
	 *            ：电子邮件
	 * @return 0 :修改失败，1: 修改成功 -2:网络异常、登录失败
	 */
	public int modifyUserInfo(String userID, String nick, String tel,
			String sex, String birthday, String email) {
		int result = 0;
		try {
			StringBuilder sb = new StringBuilder()
					.append(Common.modifyUserInfoUrl);
			sb.append("&userID=" + userID);
			sb.append("&nickname=" + TextUtil.toWeb(nick));
			sb.append("&tel=" + tel);
			sb.append("&sex=" + sex);
			sb.append("&birthday=" + birthday);
			sb.append("&email=" + email);
			String resultStr = HttpUtil.CallMethod(sb.toString());

			if (!resultStr.equals("") && resultStr != null) {
				JSONObject json = HttpUtil.toJsonObject(resultStr);
				if (json != null) {
					if (json.getString("resCode").equals("1")) {

						return result = 1;
					}
				}
			}
		} catch (Exception e) {
			return result = -2;
		}
		return result;
	}
	
	
	
	/**
	 * 加载商圈类别
	 * @param cityid:城市ID
	 * 
	 *  @return 0 :加载失败，1: 加载成功 -2:网络异常、登录失败
	 */
	public HashMap<String, Object> loadShopType(String cityid) {
		int result = 0; HashMap<String, Object> favMap = new HashMap<String, Object>();
	 
		favMap.put("result", result);
		try {
			StringBuilder sb = new StringBuilder().append(Common.LoadSQShopTypeUrl);
			sb.append("&cityid=" + TextUtil.toWeb(cityid)); 
			String resultStr = HttpUtil.CallMethod(sb.toString());  
 			if (!resultStr.equals("") && resultStr != null) {
				JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
				if (jsonObj != null) {
					if (jsonObj.getString("resCode").equals("1")) {
						String str = jsonObj.getString("ShopTypeList");	  
						result = 1;
						favMap.put("result", result);
						  insertShopType(str) ;						 
					}
				}
			}
		} catch (Exception e) {
			result = -2;
			favMap.put("result", result);
		}
		return favMap;
	}
	
	
	//两级商圈类别插入数据库
		private int insertShopType(String shoparray) throws JSONException{
			
			int result = -1;
			if (Common.dbtool==null) {
				Common.dbtool = new DbTool(ctx);	
			}	 
			if (shoparray!=null) { 
				JSONArray jsarray = new JSONArray(shoparray); 
				Common.dbtool.insertParentShopType(0531, "全部分类", "QJN");		
				Common.dbtool.insertChildShopType(0531, "全部分类", "QJN",0531, "全部分类", "QJN");		
				if (jsarray != null && jsarray.length() >= 1) {	 
					
					for (int i = 0; i < jsarray.length(); i++) {
						JSONObject json = jsarray.getJSONObject(i);
						 
						Common.dbtool.insertParentShopType(json.getInt("CategoryID"), json.getString("CategoryName"), json.getString("Letter"));					
												
						String childCategoryList = json.getString("ChildCategoryList");	
						 
						if (childCategoryList!=null) {
							JSONArray jsarray2 = new JSONArray(childCategoryList);
							if (jsarray2 != null & jsarray2.length() >= 1) {		
								Common.dbtool.insertChildShopType(json.getInt("CategoryID"), json.getString("CategoryName"), json.getString("Letter"),json.getInt("CategoryID"), json.getString("CategoryName"), json.getString("Letter"));		
							 for (int j = 0; j < jsarray2.length(); j++) {
								JSONObject json2 = jsarray2.getJSONObject(j); 
								Common.dbtool.insertChildShopType(json.getInt("CategoryID"), json.getString("CategoryName"), json.getString("Letter"),json2.getInt("CategoryID"), json2.getString("CategoryName"), json2.getString("Letter"));		
 						 
							}							
						 } 	 
					  } 						 
					}
				}  
			}  
			return result;
	}
	
		//获取一级类别
		public List<ShopParentModel>  getParentShopInfo(){
			List<ShopParentModel> pCategoryList = null;
			if (Common.dbtool==null) {
				Common.dbtool = new DbTool(ctx);	
			}
			Cursor pc = Common.dbtool.getParentShop();
//			act.startManagingCursor(pc);
			if (pc != null) {
				pCategoryList = new ArrayList<ShopParentModel>();
				for (pc.moveToFirst(); !pc.isAfterLast(); pc.moveToNext()) {
					ShopParentModel sqM = new ShopParentModel();
					sqM.setCategoryID(pc.getInt(pc.getColumnIndexOrThrow(DbHelper.KEY_PCategoryID)));
					sqM.setCategoryName(pc.getString(pc.getColumnIndexOrThrow(DbHelper.KEY_PCategoryName)));
					sqM.setLetter(pc.getString(pc.getColumnIndexOrThrow(DbHelper.KEY_PLetter))); 
					pCategoryList.add(sqM);
				}
			} 
			return pCategoryList;
		}
		
		//获取二级类别
		public List<ShopChildModel> getChildShopInfo(int PCategoryID){	 
			if (Common.dbtool==null) {
				Common.dbtool = new DbTool(ctx);	
			}
			Cursor cc = Common.dbtool.getChildShop(PCategoryID);
			 
			List<ShopChildModel> cCategoryList = null;
			if (cc != null) {
				cCategoryList = new ArrayList<ShopChildModel>();
				for (cc.moveToFirst(); !cc.isAfterLast(); cc.moveToNext()) {
					ShopChildModel sqM2 = new ShopChildModel();
					sqM2.setParentCategoryID(PCategoryID);
					sqM2.setCategoryID(cc.getInt(cc.getColumnIndexOrThrow(DbHelper.KEY_CategoryID))); 
					sqM2.setCategoryName(cc.getString(cc.getColumnIndexOrThrow(DbHelper.KEY_CategoryName)));
					sqM2.setLetter(cc.getString(cc.getColumnIndexOrThrow(DbHelper.KEY_CategoryName)));
					cCategoryList.add(sqM2);
				}					
			}
			return cCategoryList;
		}
		
		
	
	/**
	 * 加载商圈
	 * @param cityid:城市ID
	 * 
	 *  @return 0 :加载失败，1: 加载成功 -2:网络异常、登录失败
	 */
	public HashMap<String, Object> loadSQ(String cityid) {
		int result = 0; HashMap<String, Object> favMap = new HashMap<String, Object>();
//		List<SQParentModel> shopList = null;
		favMap.put("result", result);
		try {
			StringBuilder sb = new StringBuilder().append(Common.LoadSQDistrictUrl);
			sb.append("&cityid=" + TextUtil.toWeb(cityid)); 
			String resultStr = HttpUtil.CallMethod(sb.toString());  
 			if (!resultStr.equals("") && resultStr != null) {
				JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
				if (jsonObj != null) {
					if (jsonObj.getString("resCode").equals("1")) {
						String str = jsonObj.getString("ShopTypeList");	 						
//						shopList = getChildCategoryList(str);
						
						result = 1;
						favMap.put("result", result);
				    	insertSQ(str);						 
					}
				}
			}
		} catch (Exception e) {
			result = -2;
			favMap.put("result", result);
		}
		return favMap;
	}
	
	//两级商圈类别插入数据库
	private int insertSQ(String str) throws JSONException{
				int result = -1;
				if (Common.dbtool==null) {
					Common.dbtool = new DbTool(ctx);	
				}	 
				if (str!=null) {
					JSONArray jsarray = new JSONArray(str);		
					Common.dbtool.insertParentSQ(0531, "全部地区", "QJN");		
					Common.dbtool.insertChildSQ(0531, "全部地区", "QJN",0531, "全部地区", "QJN");		
					if (jsarray != null & jsarray.length() >= 1) {					 
						for (int i = 0; i < jsarray.length(); i++) {
							JSONObject json = jsarray.getJSONObject(i);
						 
							Common.dbtool.insertParentSQ(json.getInt("CategoryID"), json.getString("CategoryName"), json.getString("Letter"));					
													
							String str2 = json.getString("ChildCategoryList");		
							 
							if (str2!=null) {
								JSONArray jsarray2 = new JSONArray(str2);
								Common.dbtool.insertChildSQ(json.getInt("CategoryID"), json.getString("CategoryName"), json.getString("Letter"),json.getInt("CategoryID"), "全部"+json.getString("CategoryName"), json.getString("Letter"));		
								 	
								if (jsarray2 != null & jsarray2.length() >= 1) {							 
								 for (int j = 0; j < jsarray2.length(); j++) {
									JSONObject json2 = jsarray2.getJSONObject(j);
								 
									Common.dbtool.insertChildSQ(json.getInt("CategoryID"), json.getString("CategoryName"), json.getString("Letter"),json2.getInt("CategoryID"), json2.getString("CategoryName"), json2.getString("Letter"));		
	 						 
								}							
							 } 	 
						  } 						 
						}
					}  
				}  
				return result;
			}
//	//多级商圈
//	private List<SQModel> getChildCategoryList(String str) throws JSONException{
//		List<SQModel> childCategoryList = null;
//		if (str!=null) {
//			JSONArray jsarray = new JSONArray(str);							 
//			if (jsarray != null & jsarray.length() >= 1) {
//				childCategoryList = new ArrayList<SQModel>();
//				for (int i = 0; i < jsarray.length(); i++) {
//					JSONObject json = jsarray.getJSONObject(i);
//					SQModel sqM = new SQModel();
//					sqM.setCategoryID(json.getInt("categoryID")); 
//					sqM.setCategoryName(json.getString("categoryName"));
//					sqM.setLetter(json.getString("Letter"));
//					String str2 = json.getString("ChildCategoryList");
//					sqM.setChildCategoryList(getChildCategoryList(str2));
//					childCategoryList.add(sqM);
//				}
//			}  
//		}  
//		return childCategoryList;
//	}
	//两级商圈
//	private List<SQParentModel> getChildCategoryList(String str) throws JSONException{
//		List<SQParentModel> childCategoryList = null;
//		if (str!=null) {
//			JSONArray jsarray = new JSONArray(str);							 
//			if (jsarray != null & jsarray.length() >= 1) {
//				childCategoryList = new ArrayList<SQParentModel>();
//				for (int i = 0; i < jsarray.length(); i++) {
//					JSONObject json = jsarray.getJSONObject(i);
//					SQParentModel sqM = new SQParentModel();
//					sqM.setCategoryID(json.getInt("categoryID")); 
//					sqM.setCategoryName(json.getString("categoryName"));
//					sqM.setLetter(json.getString("Letter"));
//					String str2 = json.getString("ChildCategoryList");
//					List<SQChildModel> childCategoryList2 = null;
//					if (str2!=null) {
//						JSONArray jsarray2 = new JSONArray(str2);
//						if (jsarray2 != null & jsarray2.length() >= 1) {
//							childCategoryList2 = new ArrayList<SQChildModel>();
//							for (int j = 0; j < jsarray.length(); j++) {
//							JSONObject json2 = jsarray.getJSONObject(j);
//							SQChildModel sqM2 = new SQChildModel();
//							sqM2.setParentCategoryID(json.getInt("categoryID"));
//							sqM2.setCategoryID(json2.getInt("categoryID")); 
//							sqM2.setCategoryName(json2.getString("categoryName"));
//							sqM2.setLetter(json2.getString("Letter"));
//							childCategoryList2.add(sqM2);
//						}							
//					 } 	 
//				  }
//					sqM.setChildCategoryList(childCategoryList2);
//					childCategoryList.add(sqM);
//				}
//			}  
//		}  
//		return childCategoryList;
//	}
//	
 
	//获取一级商圈
		public List<SQParentModel>  getParentSQInfo(){
			List<SQParentModel> pCategoryList = null;
			if (Common.dbtool==null) {
				Common.dbtool = new DbTool(ctx);	
			}
			Cursor pc = Common.dbtool.getParentSQ();
//			act.startManagingCursor(pc);
			if (pc != null) {
				pCategoryList = new ArrayList<SQParentModel>();
				for (pc.moveToFirst(); !pc.isAfterLast(); pc.moveToNext()) {
					SQParentModel sqM = new SQParentModel();
					sqM.setCategoryID(pc.getInt(pc.getColumnIndexOrThrow(DbHelper.KEY_PCategoryID)));
					sqM.setCategoryName(pc.getString(pc.getColumnIndexOrThrow(DbHelper.KEY_PCategoryName)));
					sqM.setLetter(pc.getString(pc.getColumnIndexOrThrow(DbHelper.KEY_PLetter))); 
					pCategoryList.add(sqM);
				}
			} 
			return pCategoryList;
		}
		
		//获取二级商圈
		public List<SQChildModel> getChildSQInfo(int PCategoryID){	 
			if (Common.dbtool==null) {
				Common.dbtool = new DbTool(ctx);	
			}
			Cursor cc = Common.dbtool.getChildSQ(PCategoryID);
			List<SQChildModel> cCategoryList = null;
			if (cc != null) {
				cCategoryList = new ArrayList<SQChildModel>();
				for (cc.moveToFirst(); !cc.isAfterLast(); cc.moveToNext()) {
					SQChildModel sqM2 = new SQChildModel();
					sqM2.setParentCategoryID(PCategoryID);
					sqM2.setCategoryID(cc.getInt(cc.getColumnIndexOrThrow(DbHelper.KEY_CategoryID))); 
					sqM2.setCategoryName(cc.getString(cc.getColumnIndexOrThrow(DbHelper.KEY_CategoryName)));
					sqM2.setLetter(cc.getString(cc.getColumnIndexOrThrow(DbHelper.KEY_CategoryName)));
					cCategoryList.add(sqM2);
				}					
			}
			return cCategoryList;
		}
		
	
		/**
		 * 收藏店铺
		 * 
		 * @param userID     :用户ID
		 * @param pageSize   ：每页返回条数
		 * @param pageIndex  :页码索引
		 *            
		 * @return 0 :获取失败，1: 获取成功 -2:网络异常、登录失败
		 */
		public HashMap<String, Object> getShopNews(String userID, int pageSize,
				int pageIndex) {
			int result = 0;
			HashMap<String, Object> favMap = new HashMap<String, Object>();
			List<ShopNewsModel> shopList = null;
			favMap.put("result", result);
			try {
				StringBuilder sb = new StringBuilder().append(Common.ShopNewsUrl);
				sb.append("&userID=" + userID);
				sb.append("&pageSize=" + pageSize);
				sb.append("&pageIndex=" + pageIndex);

				String resultStr = HttpUtil.CallMethod(sb.toString());

				//{"resCode":"1","shopList":[{"Row":null,"UserID":0,"Distance":0.0,"CalcTime":"0001-01-01 00:00:00","SingleCoupon":null,"ShopID":1,"ShopFirstName":"李宁1","ShopSecondName":"李宁泉城路店","ShopFirstCategory":"运动","ShopSecondCategory":"休闲","ShopAddress":"泉城路18号","ShopCity":"济南","ShopDistrict":"市中区","ShopBusinessDistrict":"泉城路商圈","ShopTel":"263665","ShopLongitude":117.15863,"ShopLatitude":36.65231,"IsECardSupply":0,"ViewTimes":0,"FreezeFlag":0,"AuditFlag":1,"AuditTime":"2012-12-02 00:00:00","AuditPerson":"","Priority":0,"ShopIntro":"","EffectCouponNum":1,"UpdateTime":"2012-12-02 00:00:00","CouponUseNum":1111111,"SmallCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg","BigCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg"},{"Row":null,"UserID":0,"Distance":0.0,"CalcTime":"0001-01-01 00:00:00","SingleCoupon":null,"ShopID":2,"ShopFirstName":"安踏1","ShopSecondName":"解放路店","ShopFirstCategory":"运动","ShopSecondCategory":"健身","ShopAddress":"解放路46号","ShopCity":"济南","ShopDistrict":"历下区","ShopBusinessDistrict":"洪楼","ShopTel":"456789","ShopLongitude":117.17863,"ShopLatitude":36.11111,"IsECardSupply":0,"ViewTimes":0,"FreezeFlag":0,"AuditFlag":1,"AuditTime":"2012-12-02 00:00:00","AuditPerson":"","Priority":0,"ShopIntro":"","EffectCouponNum":8,"UpdateTime":"2012-12-02 00:00:00","CouponUseNum":2124,"SmallCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg","BigCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg"}]}
				
				if (!resultStr.equals("") && resultStr != null) {
					JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
					if (jsonObj != null) {
						if (jsonObj.getString("resCode").equals("1")) {
							String str = jsonObj.getString("WeiBoList");						 
							if (str != null && !str.equals("")) { 
								JSONArray jsarray = new JSONArray(str);							 
								if (jsarray != null & jsarray.length() >= 1) {
								 shopList = new ArrayList<ShopNewsModel>();
									for (int i = 0; i < jsarray.length(); i++) {
										JSONObject json = jsarray.getJSONObject(i);									 
										ShopNewsModel favShop = new ShopNewsModel(); 
										favShop.setSendID(json.getInt("SendID"));									 
										favShop.setShopFirstName(json.getString("ShopFirstName"));
										favShop.setShopSecondName(json.getString("ShopSecondName"));
										favShop.setMemberID(json.getInt("MemberID"));
										favShop.setWeiBoID(json.getInt("WeiBoID"));	
										favShop.setWeiBoType(json.getInt("WeiBoType"));	
										favShop.setWeiBoContent(json.getString("WeiBoContent"));
										favShop.setImageUrl(json.getString("ImageUrl"));
										favShop.setSendTime(json.getString("SendTime"));
										favShop.setSmallCoverImageUrl(json.getString("SmallCoverImageUrl"));
										favShop.setBigCoverImageUrl(json.getString("BigCoverImageUrl")); 
									   
										shopList.add(favShop);
									}
								}

								result = 1;
								favMap.put("result", result);
								favMap.put("shopList", shopList);
							}
						}
					}
				}
			} catch (Exception e) {
				result = -2;
				favMap.put("result", result);
				System.out.println(e);
			}
			return favMap;
		} 
	/**
	 * 收藏店铺
	 * 
	 * @param userID     :用户ID
	 * @param pageSize   ：每页返回条数
	 * @param pageIndex  :页码索引
	 *            
	 * @return 0 :获取失败，1: 获取成功 -2:网络异常、登录失败
	 */
	public HashMap<String, Object> getFavShop(String userID, int pageSize,
			int pageIndex) {
		int result = 0;
		HashMap<String, Object> favMap = new HashMap<String, Object>();
		List<FavShopModel> shopList = null;
		favMap.put("result", result);
		try {
			StringBuilder sb = new StringBuilder().append(Common.FavShopUrl);
			sb.append("&userID=" + userID);
			sb.append("&pageSize=" + pageSize);
			sb.append("&pageIndex=" + pageIndex);

			String resultStr = HttpUtil.CallMethod(sb.toString());

			//{"resCode":"1","shopList":[{"Row":null,"UserID":0,"Distance":0.0,"CalcTime":"0001-01-01 00:00:00","SingleCoupon":null,"ShopID":1,"ShopFirstName":"李宁1","ShopSecondName":"李宁泉城路店","ShopFirstCategory":"运动","ShopSecondCategory":"休闲","ShopAddress":"泉城路18号","ShopCity":"济南","ShopDistrict":"市中区","ShopBusinessDistrict":"泉城路商圈","ShopTel":"263665","ShopLongitude":117.15863,"ShopLatitude":36.65231,"IsECardSupply":0,"ViewTimes":0,"FreezeFlag":0,"AuditFlag":1,"AuditTime":"2012-12-02 00:00:00","AuditPerson":"","Priority":0,"ShopIntro":"","EffectCouponNum":1,"UpdateTime":"2012-12-02 00:00:00","CouponUseNum":1111111,"SmallCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg","BigCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg"},{"Row":null,"UserID":0,"Distance":0.0,"CalcTime":"0001-01-01 00:00:00","SingleCoupon":null,"ShopID":2,"ShopFirstName":"安踏1","ShopSecondName":"解放路店","ShopFirstCategory":"运动","ShopSecondCategory":"健身","ShopAddress":"解放路46号","ShopCity":"济南","ShopDistrict":"历下区","ShopBusinessDistrict":"洪楼","ShopTel":"456789","ShopLongitude":117.17863,"ShopLatitude":36.11111,"IsECardSupply":0,"ViewTimes":0,"FreezeFlag":0,"AuditFlag":1,"AuditTime":"2012-12-02 00:00:00","AuditPerson":"","Priority":0,"ShopIntro":"","EffectCouponNum":8,"UpdateTime":"2012-12-02 00:00:00","CouponUseNum":2124,"SmallCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg","BigCoverImageUrl":"/upload_xjj/1/201212301201592434111.jpg"}]}
			
			if (!resultStr.equals("") && resultStr != null) {
				JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
				if (jsonObj != null) {
					if (jsonObj.getString("resCode").equals("1")) {
						String str = jsonObj.getString("shopList");						 
						if (str != null && !str.equals("")) { 
							JSONArray jsarray = new JSONArray(str);							 
							if (jsarray != null & jsarray.length() >= 1) {
							 shopList = new ArrayList<FavShopModel>();
								for (int i = 0; i < jsarray.length(); i++) {
									JSONObject json = jsarray.getJSONObject(i);									 
									FavShopModel favShop = new FavShopModel();
									favShop.setParentShopID(json.getInt("ParentShopID"));
									favShop.setShopID(json.getInt("ShopID"));
									favShop.setParentShopName(json.getString("ParentShopName"));
									favShop.setParentShortName(json.getString("ParentShortName")); 
									favShop.setShopName(json.getString("ShopName"));
									favShop.setShopAddress (json.getString("ShopFirstCategoryID"));
									favShop.setShopTel (json.getString("ShopSecondCategoryID")); 
									favShop.setShopAddress (json.getString("ShopAddress"));
									favShop.setShopTel (json.getString("ShopTel"));  
									favShop.setShopCityID(json.getString("ShopCityID")); 
									favShop.setShopDistrictID (json.getInt("ShopDistrictID"));  
									favShop.setShopBusinessDistrictID(json.getInt("ShopBusinessDistrictID"));  
									favShop.setShopLatitude (json.getDouble("ShopLatitude")); 
									favShop.setShopLongitude (json.getDouble("ShopLongitude"));  
									favShop.setSmallCoverImageUrl(json.getString("SmallCoverImageUrl"));
									favShop.setBigCoverImageUrl(json.getString("BigCoverImageUrl"));
									 favShop.setBusinessHourOn(json.getString("BusinessHourOn"));
									 favShop.setBusinessHourOff(json.getString("BusinessHourOff")); 
									favShop.setEffectCouponNum(json.getInt("EffectCouponNum")); 
									 favShop.setPSmallCoverImageUrl(json.getString("PSmallCoverImageUrl"));
									 favShop.setPBigCoverImageUrl(json.getString("PBigCoverImageUrl"));
									 favShop.setAttentionTime(json.getString("AttentionTime")); 
									favShop.setMemberID(json.getInt("MemberID"));  
										
									 
									shopList.add(favShop);
								}
							}

							result = 1;
							favMap.put("result", result);
							favMap.put("shopList", shopList);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = -2;
			favMap.put("result", result);
		}
		return favMap;
	} 
	
	/**
	 * 优惠券搜索信息+++商圈+分类 根据ID  比如济南31 历下区3444
	 * 
	 * @param orderType     :用户ID
	 * @param pageSize   ：每页返回条数
	 * @param pageIndex  :页码索引
	 *            
	 * @return 0 :获取失败，1: 获取成功 -2:网络异常、登录失败
	 */
	public HashMap<String, Object> getDiscountInfo(String districtID,String businessDistrictID,String orderType, String shopFirstCateID,String shopSecondCateID,String couponTypeID, String imei,double lng,
			double lat,int pageSize,int pageIndex) {
		int result = 0;
		HashMap<String, Object> favMap = new HashMap<String, Object>(); 
		favMap.put("result", result); 
		try {
			/**
			 *  districtID=32&businessDistrictID=34&shopFirstCateID=11&shopSecondCateID=12&couponTypeID=15&imei=863363014570588&orderType=2&lng=117.15863&lat=36.65231&pageSize=3&pageIndex=1
			 */
			StringBuilder sb = new StringBuilder().append(Common.DiscountInfoUrl);
//			sb.append("&cityID=" + cityID);
			sb.append("&couponTypeID=" + couponTypeID);
			sb.append("&imei=" + imei);
			 
			sb.append("&orderType=" + orderType);
			sb.append("&pageSize=" + pageSize);
			sb.append("&pageIndex=" + pageIndex);
			sb.append("&lng=" + lng);
			sb.append("&lat=" + lat); 
			sb.append("&shopFirstCateID=" + shopFirstCateID);
			sb.append("&shopSecondCateID=" + shopSecondCateID); 
			sb.append("&districtID=" + districtID);
			sb.append("&businessDistrictID=" + businessDistrictID);  
			String resultStr = HttpUtil.CallMethod(sb.toString());
//			resultStr = "{'resCode':'1','CouponList':[{'Row':1,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':2,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':3,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':null}]}";
            favMap = getDiscountModel(resultStr); 
		} catch (Exception e) {
			result = -2;
			System.out.println(e);
			favMap.put("result", result);
		}
		return favMap;
	}
	/**
	 * 优惠券搜索信息+++商圈+分类 根据名字 比如济南 历下区
	 * 
	 * @param userID     :用户ID
	 * @param pageSize   ：每页返回条数
	 * @param pageIndex  :页码索引
	 *            
	 * @return 0 :获取失败，1: 获取成功 -2:网络异常、登录失败
	 */
	public HashMap<String, Object> getDiscountInfoBySQ(int searchType,int orderType,String userID, int pageSize,int pageIndex,double lng,
			double lat,	String firstCate,String secondCate,String city,String businessDistrict) {
		int result = 0;
		HashMap<String, Object> favMap = new HashMap<String, Object>(); 
		favMap.put("result", result); 
		try {
			StringBuilder sb = new StringBuilder().append(Common.DiscountInfoUrl);
			sb.append("&searchType=" + searchType);
			sb.append("&orderType=" + orderType);
			sb.append("&userID=" + userID);
			sb.append("&pageSize=" + pageSize);
			sb.append("&pageIndex=" + pageIndex);
			sb.append("&lng=" + lng);
			sb.append("&lat=" + lat); 
			sb.append("&firstCate=" + firstCate);
			sb.append("&secondCate=" + secondCate);
			sb.append("&city=" + city);
			sb.append("&businessDistrict=" + businessDistrict);

//			String resultStr = HttpUtil.CallMethod(sb.toString());
			String resultStr = HttpUtil.CallMethod(sb.toString());
//			resultStr = "{'resCode':'1','CouponList':[{'Row':1,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':2,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':3,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':null}]}";
            favMap = getDiscountModel(resultStr); 
		} catch (Exception e) {
			result = -2;
			favMap.put("result", result);
		}
		return favMap;
	}
 
	private HashMap<String, Object> getDiscountModel(String resultStr) throws JSONException{		
		int result = 0;
		HashMap<String, Object> favMap = new HashMap<String, Object>();
		ArrayList<DiscountModel> discountList = null;
		favMap.put("result", result); 
		
		if (!resultStr.equals("") && resultStr != null) {
			JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
			if (jsonObj != null) {
				if (jsonObj.getString("resCode").equals("1")) {
					String str = jsonObj.getString("CouponList");
					 
					if (str != null && !str.equals("")) {
					 
						JSONArray jsarray = new JSONArray(str);
					 
						if (jsarray != null & jsarray.length() >= 1) {
							 discountList = new ArrayList<DiscountModel>();
							for (int i = 0; i < jsarray.length(); i++) {  
								JSONObject jss2 = jsarray.getJSONObject(i);  
								DiscountModel discountModel = new DiscountModel();
								 
								discountModel.setCouponID (jss2.getInt("CouponID")); 
								discountModel.setCouponTitle (jss2.getString("CouponTitle")); 
								discountModel.setCouponDesc (jss2.getString("CouponDesc"));
//								discountModel.setCouponFirstCategory (jss2.getString("CouponFirstCategory"));
//								discountModel.setCouponSecondCategory (jss2.getString("CouponSecondCategory"));
								discountModel.setViewTimes (jss2.getInt("ViewTimes")); 
								discountModel.setUpdateTime (jss2.getString("UpdateTime")); 
								discountModel.setBigImageUrl (jss2.getString("BigImageUrl"));
								discountModel.setSmallImageUrl (jss2.getString("SmallImageUrl"));
								discountModel.setShopLongitude(jss2.getDouble("ShopLongitude"));
								discountModel.setShopLatitude(jss2.getDouble("ShopLatitude"));
								discountModel.setShopAddress(jss2.getString("ShopAddress"));
								discountModel.setShopTel(jss2.getString("ShopTel"));
								discountModel.setShopID(jss2.getString("ShopID"));
								discountModel.setShopName(jss2.getString("ShopName"));
								discountModel.setShopNum(jss2.getInt("shopNum"));
								discountModel.setDistance(jss2.getString("Distance"));
								discountModel.setParentShopID(jss2.getString("ParentShopID"));
								discountModel.setParentShopName(jss2.getString("ParentShopName")); 
								
								discountList.add(discountModel);
							}
						}

						result = 1;
						favMap.put("result", result);
						favMap.put("discountList", discountList);
					}
				}
			}
		} 
		return favMap;
	}
	
//	private HashMap<String, Object> getDiscountModel(String resultStr) throws JSONException{		
//		int result = 0;
//		HashMap<String, Object> favMap = new HashMap<String, Object>();
//		List<DiscountListModel> discountList = null;
//		favMap.put("result", result); 
//		
//		if (!resultStr.equals("") && resultStr != null) {
//			JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
//			if (jsonObj != null) {
//				if (jsonObj.getString("resCode").equals("1")) {
//					String str = jsonObj.getString("CouponList");
//					 
//					if (str != null && !str.equals("")) {
//					 
//						JSONArray jsarray = new JSONArray(str);
//					 
//						if (jsarray != null & jsarray.length() >= 1) {
//							 discountList = new ArrayList<DiscountListModel>();
//							for (int i = 0; i < jsarray.length(); i++) {
//								JSONObject json = jsarray.getJSONObject(i);    
//							 
//								DiscountListModel favShop = new DiscountListModel();
//								favShop.setRow(json.getInt("Row"));
////								favShop.setUserID(json.getInt("UserID"));
//								favShop.setDistance(json.getInt("Distance"));
////								favShop.setCalcTime(json.getString("CalcTime")); 
//								String strList = json.getString("SingleCoupon");
//								if (strList!=null) {
//								 
//									JSONObject jss2 = new JSONObject(strList);
//									FavDiscountModel discountModel = new FavDiscountModel();
//									 
//									discountModel.setCouponID (jss2.getInt("CouponID"));
//									
//									discountModel.setCouponTitle (jss2.getString("CouponTitle"));
//									
//									discountModel.setCouponDesc (jss2.getString("CouponDesc"));
//									discountModel.setCouponFirstCategory (jss2.getString("CouponFirstCategory"));
//									discountModel.setCouponSecondCategory (jss2.getString("CouponSecondCategory"));
//									discountModel.setViewTimes (jss2.getInt("ViewTimes"));
//									discountModel.setAddTime (jss2.getString("AddTime"));
//									discountModel.setUpdateTime (jss2.getString("UpdateTime"));
//									discountModel.setSort (jss2.getInt("sort"));
//									discountModel.setRecommend (jss2.getInt("recommend"));
//									discountModel.setUseIntroduction (jss2.getString("UseIntroduction"));
//									discountModel.setActivityIntroduction (jss2.getString("ActivityIntroduction"));
//									discountModel.setCouponType (jss2.getInt("CouponType"));
//									discountModel.setEffectTime (jss2.getString("EffectTime"));
//									discountModel.setExpireTime (jss2.getString("ExpireTime")); 
//									discountModel.setBigImageUrl (jss2.getString("BigImageUrl"));
//									discountModel.setSmallImageUrl (jss2.getString("SmallImageUrl"));
//									discountModel.setCouponDetailDesc (jss2.getString("CouponDetailDesc"));
//									favShop.setSingleCoupon(discountModel);
//								} 
//								favShop.setShopID(json.getInt("ShopID"));
//								favShop.setShopFirstName(json.getString("ShopFirstName"));
//								favShop.setShopSecondName(json.getString("ShopSecondName"));
//								favShop.setShopFirstCategory(json.getString("ShopFirstCategory"));
//								favShop.setShopSecondCategory(json.getString("ShopSecondCategory"));
//								favShop.setShopAddress(json.getString("ShopAddress"));
//								favShop.setShopCity(json.getString("ShopCity"));
//								favShop.setShopDistrict(json.getString("ShopDistrict"));
//								favShop.setShopBusinessDistrict(json.getString("ShopBusinessDistrict"));
//								favShop.setShopTel(json.getString("ShopTel"));
//								favShop.setShopLongitude(json.getDouble("ShopLongitude"));
//								favShop.setShopLatitude(json.getDouble("ShopLatitude"));
//								favShop.setIsECardSupply(json.getInt("IsECardSupply"));
//								favShop.setViewTimes(json.getInt("ViewTimes"));
//								favShop.setFreezeFlag(json.getInt("FreezeFlag"));
//								favShop.setAuditFlag(json.getInt("AuditFlag"));
//								favShop.setAuditTime(json.getString("AuditTime"));
//								favShop.setAuditPerson(json.getString("AuditPerson"));
//								favShop.setPriority(json.getInt("Priority"));
//								favShop.setShopIntro(json.getString("ShopIntro"));
//								favShop.setEffectCouponNum(json.getInt("EffectCouponNum"));
//								favShop.setUpdateTime(json.getString("UpdateTime"));
//								favShop.setCouponUseNum(json.getInt("CouponUseNum"));
//								
//								discountList.add(favShop);
//							}
//						}
//
//						result = 1;
//						favMap.put("result", result);
//						favMap.put("discountList", discountList);
//					}
//				}
//			}
//		} 
//		return favMap;
//	}
	
	
	/**
	 * 优惠券搜索信息+++距离+分类
	 * 
	 * @param userID     :用户ID
	 * @param pageSize   ：每页返回条数
	 * @param pageIndex  :页码索引
	 *            
	 * @return 0 :获取失败，1: 获取成功 -2:网络异常、登录失败
	 */
	public HashMap<String, Object> getDiscountInfobydistance(int searchType,int orderType,String userID, int pageSize,int pageIndex,double lng,
			double lat,	int radius,String firstCate,String secondCate) {
		int result = 0;
		HashMap<String, Object> favMap = new HashMap<String, Object>();
		ArrayList<DiscountListModel> discountList = null;
		favMap.put("result", result);
		try {
			StringBuilder sb = new StringBuilder().append(Common.DiscountInfoUrl);
			sb.append("&searchType=" + searchType);
			sb.append("&orderType=" + orderType);
			sb.append("&userID=" + userID);
			sb.append("&pageSize=" + pageSize);
			sb.append("&pageIndex=" + pageIndex);
			sb.append("&lng=" + lng);
			sb.append("&lat=" + lat);
			sb.append("&radius=" + radius);
			sb.append("&firstCate=" + firstCate);
			sb.append("&secondCate=" + secondCate);

			/**
			 * http://www.x99local.com/CouponHandler.ashx?op=couponSearch&searchType=1&orderType=1&userID=1&pageSize=3&pageIndex=1&lng=117.15863&lat=36.65231&radius=100000&firstCate=%E8%BF%90%E5%8A%A8&secondCate=%E4%BC%91%E9%97%B2
			 */
			String resultStr = HttpUtil.CallMethod(sb.toString());
//			resultStr = "{'resCode':'1','CouponList':[{'Row':1,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':2,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':3,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':null}]}";
 
			if (!resultStr.equals("") && resultStr != null) {
				JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
				if (jsonObj != null) {
					if (jsonObj.getString("resCode").equals("1")) {
						String str = jsonObj.getString("CouponList");
					 
						if (str != null && !str.equals("")) { 
							JSONArray jsarray = new JSONArray(str);
						 
							if (jsarray != null & jsarray.length() >= 1) {
								 discountList = new ArrayList<DiscountListModel>();
								for (int i = 0; i < jsarray.length(); i++) {
									JSONObject json = jsarray.getJSONObject(i);    
								 
									DiscountListModel favShop = new DiscountListModel();
									favShop.setRow(json.getInt("Row"));
//									favShop.setUserID(json.getInt("UserID"));
									favShop.setDistance(json.getInt("Distance"));
//									favShop.setCalcTime(json.getString("CalcTime")); 
									String strList = json.getString("SingleCoupon");
									if (strList!=null) {
									 
										JSONObject jss2 = new JSONObject(strList);
										FavDiscountModel discountModel = new FavDiscountModel();
										 
										discountModel.setCouponID (jss2.getInt("CouponID"));
										discountModel.setCouponDesc (jss2.getString("CouponDesc"));
										 
										discountModel.setViewTimes (jss2.getInt("ViewTimes"));
									 
										discountModel.setUpdateTime (jss2.getString("UpdateTime"));
										 
										 
										discountModel.setBigImageUrl (jss2.getString("BigImageUrl"));
										discountModel.setSmallImageUrl (jss2.getString("SmallImageUrl"));
									 
//										favShop.setSingleCoupon(discountModel);
									} 
									favShop.setShopID(json.getInt("ShopID"));
									favShop.setShopFirstName(json.getString("ShopFirstName"));
									favShop.setShopSecondName(json.getString("ShopSecondName"));
									favShop.setShopFirstCategory(json.getString("ShopFirstCategory"));
									favShop.setShopSecondCategory(json.getString("ShopSecondCategory"));
									favShop.setShopAddress(json.getString("ShopAddress"));
									favShop.setShopCity(json.getString("ShopCity"));
									favShop.setShopDistrict(json.getString("ShopDistrict"));
									favShop.setShopBusinessDistrict(json.getString("ShopBusinessDistrict"));
									favShop.setShopTel(json.getString("ShopTel"));
									favShop.setShopLongitude(json.getDouble("ShopLongitude"));
									favShop.setShopLatitude(json.getDouble("ShopLatitude"));
									favShop.setIsECardSupply(json.getInt("IsECardSupply"));
									favShop.setViewTimes(json.getInt("ViewTimes"));
									favShop.setFreezeFlag(json.getInt("FreezeFlag"));
									favShop.setAuditFlag(json.getInt("AuditFlag"));
									favShop.setAuditTime(json.getString("AuditTime"));
									favShop.setAuditPerson(json.getString("AuditPerson"));
									favShop.setPriority(json.getInt("Priority"));
									favShop.setShopIntro(json.getString("ShopIntro"));
									favShop.setEffectCouponNum(json.getInt("EffectCouponNum"));
									favShop.setUpdateTime(json.getString("UpdateTime"));
									favShop.setCouponUseNum(json.getInt("CouponUseNum"));
									
									discountList.add(favShop);
								}
							}

							result = 1;
							favMap.put("result", result);
							favMap.put("discountList", discountList);
						}
					}
				}
			}
		} catch (Exception e) {
			result = -2;
			favMap.put("result", result);
			System.out.println(e);
		}
		return favMap;
	}
	
	
	//String str = "{'resCode':'1','CouponList':[{'Row':1,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':2,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':3,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':null}]}";
	 
	/**
	 * 收藏优惠券
	 * 
	 * @param userID     :用户ID
	 * @param pageSize   ：每页返回条数
	 * @param pageIndex  :页码索引
	 *            
	 * @return 0 :获取失败，1: 获取成功 -2:网络异常、登录失败
	 */
	public HashMap<String, Object> getFavDiscountInfo(String userID,String imei ,String lng,String lat, int pageSize,int pageIndex) {
		int result = 0;
		HashMap<String, Object> favMap = new HashMap<String, Object>();
		ArrayList<FavDiscountModel> discountList = null;
		favMap.put("result", result);
		try {
			StringBuilder sb = new StringBuilder().append(Common.FavDiscountUrl);
			sb.append("&userID=" + userID);
			sb.append("&imei=" + imei);
			sb.append("&lng=" + lng);
			sb.append("&lat=" + lat); 
			sb.append("&pageSize=" + pageSize);
			sb.append("&pageIndex=" + pageIndex);
//
			String resultStr = HttpUtil.CallMethod(sb.toString());
//			resultStr = "{'resCode':'1','CouponList':[{'Row':1,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':2,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':'1900-01-01T00:00:00'},{'Row':3,'UserID':1,'CouponID':1,'CouponDesc':'仅售25元,价值70元电影票1张!含3D!各路明星各展演技,逼真音效震撼全场!一次精彩邂逅,一段美好回忆!','CouponFirstCategory':'优惠劵一级分类1','CouponSecondCategory':'优惠劵二级分类1','ViewTimes':0,'AddTime':'2012-12-25T19:33:42','UpdateTime':'2013-02-25T22:11:09.25','sort':0,'recommend':0,'UseIntroduction':'','ActivityIntroduction':'','CouponType':1,'EffectTime':'2012-12-06T19:31:45','ExpireTime':'2015-12-28T19:31:49','BigImageUrl':'/upload_xjj/1/201302252207570479508.png','SmallImageUrl':'/upload_xjj/1/201302252207421728820.jpg','CouponDetailDesc':'','FavoriteTime':null}]}";
 
			if (!resultStr.equals("") && resultStr != null) {
				JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
				if (jsonObj != null) {
					if (jsonObj.getString("resCode").equals("1")) {
						String str = jsonObj.getString("CouponList");
						 
						if (str != null && !str.equals("")) {
						 
							JSONArray jsarray = new JSONArray(str);
						 
							if (jsarray != null & jsarray.length() >= 1) {
								 discountList = new ArrayList<FavDiscountModel>();
								for (int i = 0; i < jsarray.length(); i++) { 
									discountList.add(getDiscountInfoModel(jsarray.getJSONObject(i)));
								}
							}
							result = 1;
							favMap.put("result", result);
							favMap.put("discountList", discountList);
						}
					}
				}
			}
		} catch (Exception e) {
			result = -2;
			favMap.put("result", result);
		}
		return favMap;
	}
	
	private FavDiscountModel getDiscountInfoModel(JSONObject json) throws JSONException{
		
		FavDiscountModel favShop = new FavDiscountModel();
		favShop.setRow(json.getInt("Row"));
		favShop.setCouponTitle(json.getString("CouponTitle"));
		favShop.setCouponID (json.getInt("CouponID"));
		favShop.setCouponDesc (json.getString("CouponDesc")); 
		favShop.setViewTimes (json.getInt("ViewTimes"));
//		favShop.setAddTime (json.getString("AddTime"));
		favShop.setUpdateTime (json.getString("UpdateTime"));
		favShop.setBigImageUrl (json.getString("BigImageUrl"));
		favShop.setSmallImageUrl (json.getString("SmallImageUrl")); 
 
		favShop.setParentShopID(json.getInt("ParentShopID"));
		favShop.setParentShopName(json.getString("ParentShopName"));
		favShop.setShopID(json.getInt("ShopID"));
		favShop.setShopName(json.getString("ShopName"));
		favShop.setDistance (json.getString("Distance")); 
		favShop.setShopAddress (json.getString("ShopAddress"));
		favShop.setShopTel (json.getString("ShopTel")); 
		favShop.setShopLatitude (json.getDouble("ShopLatitude")); 
		favShop.setShopLongitude (json.getDouble("ShopLongitude"));  
		
		return favShop;
	}
	
	
	/**
	 * 优惠券详情
	 * 
	 * @param couponID     :优惠券Id
	 *            
	 * @return 0 :获取失败，1: 获取成功  -2:网络异常、解析失败
	 */
	public HashMap<String, Object> getDiscountDetail(String couponID) {
		int result = 0;
		HashMap<String, Object> favMap = new HashMap<String, Object>();
		 
		favMap.put("result", result);
		try {
			StringBuilder sb = new StringBuilder().append(Common.DiscountDetailUrl);
			sb.append("&couponID=" + couponID); 

			String resultStr = HttpUtil.CallMethod(sb.toString()); 
			favMap.put("resultStr", HttpUtil.toJsonObject(resultStr));
			if (!resultStr.equals("") && resultStr != null) {
				JSONObject jsonObj = HttpUtil.toJsonObject(resultStr);
				if (jsonObj != null) {
					if (jsonObj.getString("resCode").equals("1")) { 
						result = 1;
						favMap.put("result", result);
						
						String strList = jsonObj.getString("CouponInfo");
						DiscountDetailModel discountModel = null;
						if (strList!=null) {
						 
							JSONObject jss2 = new JSONObject(strList);
							discountModel = new DiscountDetailModel();
							discountModel.setCouponID(jss2.getLong("CouponID"));
							discountModel.setCouponTitle (jss2.getString("CouponTitle"));
							discountModel.setCouponDesc (jss2.getString("CouponDesc"));
							discountModel.setViewTimes (jss2.getInt("ViewTimes")); 
//							discountModel.setUpdateTime (jss2.getString("UpdateTime"));  
						 
							discountModel.setUseIntroduction (jss2.getString("UseIntroduction")); 
							discountModel.setActivityIntroduction(jss2.getString("ActivityIntroduction")); 
					 
							discountModel.setCouponType (jss2.getInt("CouponType")); 
							discountModel.setEffectTime (jss2.getString("EffectTime")); 
							discountModel.setUpdateTime (jss2.getString("ExpireTime"));  
							
							discountModel.setBigImageUrl (jss2.getString("BigImageUrl"));
							discountModel.setSmallImageUrl (jss2.getString("SmallImageUrl")); 
							discountModel.setCouponDetailDesc (jss2.getString("CouponDetailDescUrl"));  
						} 
						favMap.put("discountModel", discountModel); 
						
						String strShopList = jsonObj.getString("ShopList"); 
						ArrayList<ShopBaseInfoModel> shopList = null;				 
						if (strShopList != null && !strShopList.equals("")) { 
							JSONArray jsarray = new JSONArray(strShopList);	 
							if (jsarray != null & jsarray.length() >= 1) {
							 shopList = new ArrayList<ShopBaseInfoModel>();
								for (int i = 0; i < jsarray.length(); i++) {
									JSONObject json = jsarray.getJSONObject(i);									 
									ShopBaseInfoModel favShop = new ShopBaseInfoModel(); 
									favShop.setShopID(json.getInt("ShopID"));
//									favShop.setCouponID(json.getInt("CouponID"));
									favShop.setParentShopID(json.getInt("ParentShopID"));
									favShop.setParentShopName(json.getString("ParentShopName"));
									favShop.setParentShortName(json.getString("ParentShortName")); 
									favShop.setShopName(json.getString("ShopName"));
									
									favShop.setShopFirstCategoryID(json.getString("ShopFirstCategoryID"));
									favShop.setShopSecondCategoryID(json.getString("ShopSecondCategoryID"));
									favShop.setShopAddress (json.getString("ShopAddress"));
									favShop.setShopCityID(json.getString("ShopCityID"));
//									favShop.setDistance(json.getString("Distance"));   
									favShop.setShopAddress(json.getString("ShopAddress")); 
									favShop.setShopTel(json.getString("ShopTel")); 
									favShop.setParentShopID(json.getInt("ParentShopID"));
									favShop.setParentShopName(json.getString("ParentShopName")); 
									favShop.setShopDistrictID(json.getInt("ShopDistrictID"));
									favShop.setShopBusinessDistrictID(json.getInt("ShopBusinessDistrictID")); 
									favShop.setShopLongitude(json.getDouble("ShopLongitude"));
									favShop.setShopLatitude(json.getDouble("ShopLatitude")); 
									favShop.setViewTimes(json.getInt("ViewTimes")); 
									favShop.setShopIntro(json.getString("ShopIntro"));
									favShop.setEffectCouponNum(json.getString("EffectCouponNum"));
									favShop.setCouponUseNum(json.getString("CouponUseNum"));
									favShop.setUpdateTime(json.getString("UpdateTime"));   
									shopList.add(favShop);
								}
							}  
						}					
						favMap.put("ShopList", shopList);
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e);
			result = -2;
			favMap.put("result", result);
		}
		return favMap;
	} 
	}
